/*
 * @Author: xiaxia
 * @Description: 
 * @Date: 2024-06-11 17:47:35
 * @LastEditTime: 2024-06-26 11:08:52
 * @FilePath: \project\vite.config.ts
 */
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
import VueSetupExtend from 'vite-plugin-vue-setup-extend' // 组件name

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  let env = loadEnv(mode, process.cwd()) // 获取环境对应的变量
  console.log(env.VITE_APP_BASE_API);
  
  return {
    plugins: [
      vue(),
      VueSetupExtend(), // 组件name
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }), // svg
      viteMockServe({
        localEnabled: command === 'serve',
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src") // 相对路径别名配置，使用 @ 代替 src
      }
    },
    // scss全局
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    // 代理跨域
    server:{
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, // 服务器地址
          changeOrigin: true, // 是否代理跨域
          rewrite: (path) => path.replace(/^\/api/, ''), // 路径重写
        }
      }
    }
  }
})
